package svn.project;

import java.io.File;

import org.tmatesoft.svn.core.SVNCancelException;
import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNException;

import org.tmatesoft.svn.core.wc.ISVNEventHandler;
import org.tmatesoft.svn.core.wc.SVNCommitClient;
import org.tmatesoft.svn.core.wc.SVNCommitPacket;
import org.tmatesoft.svn.core.wc.SVNEvent;
import org.tmatesoft.svn.core.wc.SVNEventAction;

public class SVNCommitManager  {
	
	public SVNCommitManager(String url) {
		initSvnCommitManger(url);
		actionDilog = new EventActionDilog("Commit");
	}
	
	public void initSvnCommitManger(String url) {
		SETClientManager scm = new SETClientManager(url);
		commitClient = scm.getSVNCommitClient();
		commitClient.setEventHandler(new MyCustomUpdateEventHandler());
		isStop = true;
		//connectManager = new SVNConnectManager(url);
		//connectManager.initSvnManger(url);
	}
	
	public void commitItems(final SVNCommitPacket scPacket, final String message) {
		Runnable svnTreeRun = new Runnable() {
			public void run() {
				while(isStop) {
					try{
						Thread.sleep(20);
					} catch(InterruptedException ie) {
					}
					doCommit(scPacket, message);
				}
			}
		};
		showDilogThread = new Thread(svnTreeRun, "showDilogThread");
		showDilogThread.start();
	}
	
	
	public void doCommit(SVNCommitPacket scPacket, String message) {
		try {
			SVNCommitInfo scommitInfo = commitClient.doCommit(scPacket, false, message);
			System.out.println(scommitInfo.getNewRevision());
			if(scommitInfo.getNewRevision() == -1)
				isStop = false;
			System.out.println("OK...");
		} catch(SVNException svne) {
			System.out.println("Error(s): " + svne + "in Method doCommit(...)");
			String ErrorMessage = svne.getMessage();
			String tmp = "";
			actionDilog.setFont();
			while(ErrorMessage.indexOf("svn: ") != -1) {
				ErrorMessage = ErrorMessage.substring(ErrorMessage.indexOf("svn: ") + "svn: ".length());
				int n = ErrorMessage.indexOf("svn: ");
				if(n != -1)
					tmp = ErrorMessage.substring(0, ErrorMessage.indexOf("svn: "));
				else
					tmp = ErrorMessage.substring(0);
				String[] insertInto = {"Error", tmp};
				actionDilog.insertData(insertInto);
			}
			actionDilog.show();
			isStop = false;
		}
	}
	
	public SVNCommitPacket getCommitPacket(String filePath) {
		SVNCommitPacket scPacket = null;
		File file = new File (filePath);
		try{
			scPacket = commitClient.doCollectCommitItems(new File[] {file}, true, false, true);	
		} catch(SVNException svne) {
			System.err.println("Error(s)-: "+svne + "in getCommitPacket");
		}
		return scPacket;
	}
	
	private SVNCommitClient commitClient;
	private EventActionDilog actionDilog;
	private Thread showDilogThread;
	private boolean isStop;
	//private SVNConnectManager connectManager;
	private class MyCustomUpdateEventHandler implements ISVNEventHandler{
		public void checkCancelled() throws SVNCancelException {
		}
		public void handleEvent(SVNEvent event, double progress) throws SVNException {
			
			String eventAction = null;
			if(event.getAction() == SVNEventAction.COMMIT_ADDED) {
				//kind = event.getNodeKind();
				eventAction = "Added";
				System.out.println("ADDED");
			}
			else if(event.getAction() == SVNEventAction.COMMIT_COMPLETED) {
				eventAction = "Completed";
				actionDilog.reSetTitle();
				isStop = false;
				actionDilog.resetVisible();
				System.out.println("COMPLETED");
			}
			else if(event.getAction() == SVNEventAction.COMMIT_DELETED) {
				eventAction = "Deleted";
				System.out.println("DELETED");
			}
			else if(event.getAction() == SVNEventAction.COMMIT_DELTA_SENT) {
				eventAction = "Data Senting";
				System.out.println("Data sent");
			}
			else if(event.getAction() == SVNEventAction.COMMIT_MODIFIED) {
				eventAction = "Modified";
				System.out.println("MODIFIED");
			}
			else if(event.getAction() == SVNEventAction.COMMIT_REPLACED) {
				eventAction = "Replaced";
				System.out.println("REPLACED");
			}
			if(actionDilog != null)
				actionDilog.show();
			String[] insertInto = {eventAction, event.getPath()};
			actionDilog.insertData(insertInto);
    		System.out.println(event.getPath());
		}
	}
	
}